package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.synctrade.parameter.mongo.Order;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.ordersync.service.SyncVerifyService;
import com.ymatou.iapi.trading.sellerquery2.parameter.GetCustomServiceOrderInfoListReq;
import com.ymatou.iapi.trading.sellerquery2.parameter.OrderRepository;
import com.ymatou.iapi.trading.sellerquery2.parameter.resp.GetCustomServiceOrderInfoListResp;
import com.ymatou.iapi.trading.sellerquery2.parameter.resp.OrderInfo;
import com.ymatou.iapi.trading.sellerquery2.service.GetCustomServiceOrderInfoListCall;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

/**
 * 订单查询请求(客服团队)新增根据用户查询明细的接口
 * 
 */
@TestSuite
public class Ts_GetCustomServiceOrderInfoList {

	private static GetCustomServiceOrderInfoListReq getCustomServiceOrderListBean;
	private static GetCustomServiceOrderInfoListCall getCustomServiceOrderListCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单查询请求(客服团队)新增根据用户查询明细的接口");
	}

	@Before
	public void caseUp() {
		getCustomServiceOrderListBean = new GetCustomServiceOrderInfoListReq();
		getCustomServiceOrderListCall = new GetCustomServiceOrderInfoListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getCustomServiceOrderListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	// 根据总记录数，每页数 计算总共有多少页
	private int getTotalPage(int totalRecords, int pageSize) {
		int totalPage;
		if (totalRecords % pageSize > 0) {
			totalPage = (totalRecords / pageSize) + 1;
		} else {
			totalPage = (totalRecords / pageSize);
		}
		return totalPage;
	}

	private void verify() {

		GetCustomServiceOrderInfoListResp actual = getCustomServiceOrderListCall
				.getCustomServiceOrderInfoListResp();

		if (actual.getOrderList().size() < 1) {
			DataUtil.verify(null, actual.getOrderList(), "验证期望的orderList为null");
		} else {
			boolean goon = actual.getOrderList().size() > 0;

			DataUtil.verify(true, goon, "验证期望的Order ！= null");
			if (goon) {

				verifyOrderList(actual.getOrderList());

			}
		}
	}
	private void verifyOrderList(List<OrderInfo> actual) {

		for (int i = 0; i < actual.size(); i++) {

			OrderInfo actualOrderInfo = actual.get(i);

			Long orderId = actualOrderInfo.getOrderId();
			Logger.comment("校验订单号orderId:" + orderId);

			Order order = OrderRepository.getOrderFromMongo(orderId);

			OrderInfo expectOrderInfo = OrderRepository.convertOrder(order);

			DataUtil.verifyObject(expectOrderInfo, actualOrderInfo, "校验数据返回");

		}

	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetCustomServiceOrderInfoList_001() {
		Logger.start(true, "客服后台根据orderId 列表获取订单详情成功");
		try {

			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));

			Logger.comment("准备订单数据");

			OrderCreate o = new OrderCreate();

			int orderId = o.create4_ReceivedOrder(userId, sellerId)
					.getiOrderId();

			SyncVerifyService.getSyncResult(orderId);

			Logger.comment("完成准备订单数据");

			getCustomServiceOrderListBean.setUserId(userId);

			getCustomServiceOrderListBean.setPageNum(1);

			getCustomServiceOrderListBean.setPageSize(1);

			getCustomServiceOrderListCall
					.setData(getCustomServiceOrderListBean);

			getCustomServiceOrderListCall.callService();

			Logger.verifyEquals("200", getCustomServiceOrderListCall.getCode(),
					"验证code");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetCustomServiceOrderInfoList_002() {
		Logger.start(true, "一个订单下有多个商品,客服后台根据orderId获取订单详情成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrder = PlaceOrderService.placeOrder4();

			int userId = placeOrder.getUserId();
			int sellerId = placeOrder.getOrderList().get(0).getSellerId();

			int tradingId = placeOrder.getTradingId();
			Integer orderId = placeOrder.getOrderList().get(0).getOrderId();

			BigDecimal price = placeOrder.getPayAmount();

			new OrderCallService().orderPayRequest(userId, price, tradingId);

			new OrderCallService().acceptOrder(orderId, sellerId);

			new OrderCallService().dispatched(orderId, sellerId);

			new OrderCallService().confirmReceive(userId, orderId);

			Logger.comment("前置数据准备结束");

			SyncVerifyService.getSyncResult(orderId);

			getCustomServiceOrderListBean.setUserId(userId);

			getCustomServiceOrderListBean.setPageNum(1);

			getCustomServiceOrderListBean.setPageSize(1);

			getCustomServiceOrderListCall
					.setData(getCustomServiceOrderListBean);

			getCustomServiceOrderListCall.callService();

			Logger.verifyEquals("200", getCustomServiceOrderListCall.getCode(),
					"验证code");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetCustomServiceOrderInfoList_003() {
		Logger.start(true, "根据订单开始截止时间，查询订单成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrder = PlaceOrderService.placeOrder4();

			int userId = placeOrder.getUserId();

			Logger.comment("前置数据准备结束");

			getCustomServiceOrderListBean.setUserId(userId);

			getCustomServiceOrderListBean.setPageNum(1);

			getCustomServiceOrderListBean.setPageSize(25);

			getCustomServiceOrderListBean.setBeginTime(YMTDateUtil
					.getBeforeOrNextMonth(-1, "yyyy-MM-dd'T'HH:mm:ss"));

			getCustomServiceOrderListBean.setEndTime(YMTDateUtil
					.getBeforeOrNextMonth(0, "yyyy-MM-dd'T'HH:mm:ss"));

			getCustomServiceOrderListCall
					.setData(getCustomServiceOrderListBean);

			getCustomServiceOrderListCall.callService();

			Logger.verifyEquals("200", getCustomServiceOrderListCall.getCode(),
					"验证code");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetCustomServiceOrderInfoList_004() {
		Logger.start(true, "根据订单开始截止时间加订单状态，查询订单成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrder = PlaceOrderService.placeOrder4();

			int userId = placeOrder.getUserId();

			Logger.comment("前置数据准备结束");

			Thread.sleep(3000);

			getCustomServiceOrderListBean.setUserId(userId);

			getCustomServiceOrderListBean.setPageNum(1);

			getCustomServiceOrderListBean.setPageSize(25);

			getCustomServiceOrderListBean.setTradingStatuses(Arrays.asList(1,
					2, 4));

			getCustomServiceOrderListBean.setBeginTime(YMTDateUtil
					.getBeforeOrNextMonth(-1, "yyyy-MM-dd'T'HH:mm:ss"));

			getCustomServiceOrderListBean.setEndTime(YMTDateUtil
					.getBeforeOrNextMonth(0, "yyyy-MM-dd'T'HH:mm:ss"));

			getCustomServiceOrderListCall
					.setData(getCustomServiceOrderListBean);

			getCustomServiceOrderListCall.callService();

			Logger.verifyEquals("200", getCustomServiceOrderListCall.getCode(),
					"验证code");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetCustomServiceOrderInfoList_005() {
		Logger.start(true, "PageNum超过1000");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrder = PlaceOrderService.placeOrder4();

			int userId = placeOrder.getUserId();

			Logger.comment("前置数据准备结束");

			getCustomServiceOrderListBean.setUserId(userId);

			getCustomServiceOrderListBean.setPageNum(1001);

			getCustomServiceOrderListBean.setPageSize(101);

			getCustomServiceOrderListBean.setBeginTime(YMTDateUtil
					.getBeforeOrNextMonth(-1, "yyyy-MM-dd'T'HH:mm:ss"));

			getCustomServiceOrderListBean.setEndTime(YMTDateUtil
					.getBeforeOrNextMonth(0, "yyyy-MM-dd'T'HH:mm:ss"));

			getCustomServiceOrderListCall
					.setData(getCustomServiceOrderListBean);

			getCustomServiceOrderListCall.callService();

			Logger.verifyEquals("400", getCustomServiceOrderListCall.getCode(),
					"验证code");

			Logger.verifyEquals("最大请求页数不能超过1000页",
					getCustomServiceOrderListCall.getMsg(), "验证msg");

			//verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetCustomServiceOrderInfoList_006() {
		Logger.start(true, "最大记录数超过10000");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrder = PlaceOrderService.placeOrder4();

			int userId = placeOrder.getUserId();

			Logger.comment("前置数据准备结束");

			getCustomServiceOrderListBean.setUserId(userId);

			getCustomServiceOrderListBean.setPageNum(100);

			getCustomServiceOrderListBean.setPageSize(101);

			getCustomServiceOrderListBean.setBeginTime(YMTDateUtil
					.getBeforeOrNextMonth(-1, "yyyy-MM-dd'T'HH:mm:ss"));

			getCustomServiceOrderListBean.setEndTime(YMTDateUtil
					.getBeforeOrNextMonth(0, "yyyy-MM-dd'T'HH:mm:ss"));

			getCustomServiceOrderListCall
					.setData(getCustomServiceOrderListBean);

			getCustomServiceOrderListCall.callService();

			Logger.verifyEquals("400", getCustomServiceOrderListCall.getCode(),
					"验证code");

			Logger.verifyEquals("最大请求数量不能超过10000条",
					getCustomServiceOrderListCall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
